package com.teamtgv.shareshark;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.teamtgv.shareshark.R;
import com.facebook.Session;
import com.teamtgv.shareshark.result.Result;
import com.teamtgv.shareshark.task.GetRecentRecommendationsTask;

public class MainFragment extends Fragment {

	ProgressDialog pDialog;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_main_fragment,
				container, false);

		pDialog = new ProgressDialog(this.getActivity());
		loadFacebookData();
		
		 TextView t2 = (TextView) view.findViewById(R.id.buttonFeedback);
		 t2.setMovementMethod(LinkMovementMethod.getInstance());

		Button shareButton = (Button) view.findViewById(R.id.buttonShareScreen);
		shareButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				loadFacebookData();
				Intent myIntent = new Intent(v.getContext(),
						ShareActivity.class);
				MainFragment.this.startActivity(myIntent);
			}
		});

		// add listener to ask button
		Button askButton = (Button) view.findViewById(R.id.buttonAskScreen);
		askButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				loadFacebookData();
				Intent myIntent = new Intent(v.getContext(), AskActivity.class);
				MainFragment.this.startActivity(myIntent);
			}
		});

		// add listener to recent button to display static list (no real data).
		Button recentButton = (Button) view
				.findViewById(R.id.buttonRecentScreen);
		recentButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				loadFacebookData();
				GetRecentRecommendationsTask recentTask = new GetRecentRecommendationsTask(
						v.getContext());
				recentTask.execute();
				try {
					ArrayList<Result> results = recentTask.get();
					if (results == null) {
						Toaster.showToast(
								MainFragment.this.getActivity(),
								"The recommendations could not be loaded. Please check your internet connection.");
					} else {
						Intent myIntent = new Intent(v.getContext(),
								ResultListActivity.class);
						myIntent.putParcelableArrayListExtra(
								ResultListActivity.RESULT_LIST, results);
						MainFragment.this.startActivity(myIntent);
					}
				} catch (Exception e) {
					e.printStackTrace();
					Log.v("MainActivity", e.toString() + ": " + e.getMessage());
				}

			}
		});

		return view;
	}

	private void loadFacebookData() {
		Log.v("MainFragment", "Data loaded?: " + AppData.facebookLoaded);
		if (!AppData.facebookLoaded && Session.getActiveSession() != null) {
			InternetChecker internetChecker = new InternetChecker(
					this.getActivity());
			if (internetChecker.isOnline() && internetChecker.serverReachable()) {
				pDialog.setMessage("Loading data...");
				pDialog.setIndeterminate(false);
				pDialog.setCancelable(false);
				pDialog.show();

				Log.v("MainFragment", "Loading data from FB");
				UserData.loadData(Session.getActiveSession(), this);
			}
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		pDialog.dismiss();
	}
	
	public void callbackDataLoaded() {
		AppData.facebookLoaded = true;
		pDialog.dismiss();
	}

}
